package com.mrjoew.weekend.web.sysapi;

import com.mrjoew.weekend.constants.SecurityConstant;
import com.mrjoew.weekend.entities.WeekendUserDetails;
import com.mrjoew.weekend.entities.vo.UserInfoVO;
import io.swagger.annotations.Api;
import org.modelmapper.ModelMapper;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Christopher Jones
 * @ClassName SysApiController
 * @explain
 * @date 2020/6/20 0020 16:05
 */
@Api(tags = "系统")
@RequestMapping(value = "/sysapi")
@RestController
public class SysApiController {

    /**
     * 获取当前用户信息
     *
     * @param authentication 当前用户信息
     * @return UserVo
     */
    @GetMapping(value = "/user")
    public UserInfoVO user(Authentication authentication) {
        WeekendUserDetails userDetails = null;
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal.equals(SecurityConstant.ANONYMOUS_USER)) {
                return null;
            }
            userDetails = (WeekendUserDetails) principal;
        }

        if (userDetails != null) {
            return new ModelMapper().map(userDetails, UserInfoVO.class);
        }

        return null;
    }
}
